package com.quad.innovators.salesease.aspect;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.context.DataPermissionContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 处理数据权限的切面
 *
 */
@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class DataPermissionAspect {
    /**
     * 数据权限切点
     */
    @Pointcut(value = "@annotation(dataPermission)")
    public void preventDataPermissionPointCut(DataPermission dataPermission) {}


    /**
     * 前置切面
     * @param dataPermission 数据权限
     */
    @Before(value = "preventDataPermissionPointCut(dataPermission)", argNames = "dataPermission")
    public void beforeMethod(DataPermission dataPermission) {
        DataPermissionContextHolder.set(dataPermission);
    }


    /**
     * 后置切面
     * @param dataPermission 数据权限
     */
    @After(value = "preventDataPermissionPointCut(dataPermission)", argNames = "dataPermission")
    public void afterMethod(DataPermission dataPermission) {
        DataPermissionContextHolder.clear();
    }
}
